package cn.virens.common.project.generator.database.datasource.types;

import cn.virens.common.util.exception.APIException;
import org.apache.ibatis.type.JdbcType;

public enum JdbTypes {
    /**
     * CHAR
     */
    CHAR(JdbcType.CHAR.name(), "Char", null),
    /**
     * BIT
     */
    BIT(JdbcType.BIT.name(), "Boolean", null),
    /**
     * BLOB
     */
    BLOB(JdbcType.BLOB.name(), "Byte[]", null),
    /**
     * Float
     */
    FLOAT(JdbcType.FLOAT.name(), "Float", null),
    /**
     * BIGINT
     */
    BIGINT(JdbcType.BIGINT.name(), "Long", null),
    /**
     * INT
     */
    INT(JdbcType.INTEGER.name(), "Integer", null),
    /**
     * TEXT
     */
    TEXT(JdbcType.VARCHAR.name(), "String", null),
    /**
     * BINARY
     */
    BINARY(JdbcType.BINARY.name(), "Byte[]", null),
    /**
     * DOUBLE
     */
    DOUBLE(JdbcType.DOUBLE.name(), "Double", null),
    /**
     * TINYINT
     */
    TINYINT(JdbcType.TINYINT.name(), "Boolean", null),
    /**
     * VARCHAR
     */
    VARCHAR(JdbcType.VARCHAR.name(), "String", null),
    /**
     * INTEGER
     */
    INTEGER(JdbcType.INTEGER.name(), "Integer", null),
    /**
     * SMALLINT
     */
    SMALLINT(JdbcType.SMALLINT.name(), "Short", null),
    /**
     * DATE
     */
    DATE(JdbcType.DATE.name(), "LocalDate", "java.time.LocalDate"),
    /**
     * TIME
     */
    TIME(JdbcType.TIME.name(), "LocalTime", "java.time.LocalTime"),
    /**
     * NUMERIC
     */
    NUMERIC(JdbcType.NUMERIC.name(), "BigDecimal", "java.math.BigDecimal"),
    /**
     * DECIMAL
     */
    DECIMAL(JdbcType.DECIMAL.name(), "BigDecimal", "java.math.BigDecimal"),
    /**
     * DATETIME
     */
    DATETIME(JdbcType.TIMESTAMP.name(), "LocalDateTime", "java.time.LocalDateTime"),
    /**
     * TIMESTAMP
     */
    TIMESTAMP(JdbcType.TIMESTAMP.name(), "LocalDateTime", "java.time.LocalDateTime");

    private final String type;
    private final String typeName;
    private final String typePackage;

    private JdbTypes(String type, String typeName, String typePackage) {
        this.typePackage = typePackage;
        this.typeName = typeName;
        this.type = type;
    }

    public static JdbTypes pasre(String type) {
        for (JdbTypes t : values()) {
            if (t.eq(type)) {
                return t;
            }
        }

        return VARCHAR;
    }

    public boolean eq(String t) throws APIException {
        return t != null && name().equalsIgnoreCase(t);
    }

    public String getType() {
        return type;
    }

    public String getTypeName() {
        return typeName;
    }

    public String getTypePackage() {
        return typePackage;
    }

}
